const service = require('../../../utils/service.js')
const api = require('../../../utils/util.js')
Page({
  data: {
    yzc_status: 0,
    withdrawMoney: '',
    isApply: true
  },
  init: function() {
    let self = this,
      shop_code = wx.getStorageSync('shopInfo').shop_code,
      shopData = wx.getStorageSync('shop_data');
    api.ajax(service.url('getWithdrawMoney'), {
      unionid: shopData.shop_unionid,
      token: shopData.token,
      token_key: shopData.token_key, 
      shop_code: shop_code,
      isWithdraw: 0,
      yzc_status: self.data.yzc_status
    }, function(res) {
      console.log("可提现：" + JSON.stringify(res))
      if (res.code == 0) {
        self.setData({
          withdrawMoney: (+res.data).toFixed(2),
          isApply: res.data == 0 || res.data == '' ? false : true
        });
      } else {
        api.toast(res.message || '数据出现错误！');
      }
    });
  },
  withdraw: function() {
    if (!this.data.isApply) {
      api.toast(this.data.yzc_status == 1 ? "没有可提现的YZC！" : "没有可提现金额！");
      return;
    }
    let self = this,
      shop_code = wx.getStorageSync('shopInfo').shop_code,
      shopData = wx.getStorageSync('shop_data');
    wx.showModal({
      title: "温馨提示",
      content: '请确认是否提现？',
      success: function(res) {
        console.log(res);
        if (res.confirm) {
          api.ajax(service.url('doWithdrawMoney'), {
            unionid: shopData.shop_unionid,
            token: shopData.token,
            token_key: shopData.token_key, 
            shop_code: shop_code,
            yzc_status: self.data.yzc_status
          }, function(res) {
            console.log("可提现：" + JSON.stringify(res.data))
            if (res.code == 0) {
              let txt = "提现申请成功！" + (self.data.yzc_status == 1 ? 'yzc后台审核后可到账' : '现金确认提现后2个小时内提交上银行，预计24小时到账，实际到账时间以银行为准。');
              wx.showModal({
                showCancel: false,
                content: txt,
                complete: function() {
                  wx.navigateBack();
                }
              });
            } else {
              api.toast(res.message || '数据出现错误！');
            }
          });
        } else {
          api.toast("取消提现！");
        }
      },
      fail: function() {
        api.toast("微信API弹窗调用失败！");
      }
    });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      yzc_status: +options.yzc_status
    });
    wx.setNavigationBarTitle({
      title: options.title //页面标题为路由参数
    })
    this.init();
  },
  /**
   * 页面显示/切入前台时触发
   */
  onShow: function() {

  }
})